//
//  ViewController.m
//  PlayerD0mo
//
//  Created by rimi on 16/5/27.
//  Copyright © 2016年 薛涛. All rights reserved.
//

#import "ViewController.h"
#import "SNPlayerView.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UISlider *slider;
@property (nonatomic, strong)  SNPlayerView *player;
@property (strong, nonatomic) IBOutlet UIProgressView *progress;
@property (strong, nonatomic) IBOutlet UITextField *time;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //	NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"a" ofType:@"mov"]];
    //    NSURL *url = [NSURL URLWithString:@"http://m3u8i.vodfile.m1905.com/201604051413/493448f06a779aabf77003c2dc61a140/movie/2015/01/29/m20150129X1R8SZ764PVKKZSC/m20150129X1R8SZ764PVKKZSC.m3u8"];
    self.player = [[SNPlayerView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 250)];
    NSURL *url = [NSURL URLWithString:@"http://content.viki.com/test_ios/ios_240.m3u8"];
    self.player.playUrl = url;
    [self.view addSubview:self.player];
    
    //	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 15 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    //		self.player.center = self.view.center;
    //	});
    [self.player addObserver:self forKeyPath:@"totalTime" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    [self.player addObserver:self forKeyPath:@"bufferTime" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    [self.player addObserver:self forKeyPath:@"currentTime" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    [self.slider addTarget:self action:@selector(jumpValue) forControlEvents:UIControlEventTouchUpInside];
    [self.slider addTarget:self action:@selector(changValue) forControlEvents:UIControlEventValueChanged];
    
    [self.player addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)]];
}

- (void)touchesBegan:(NSSet <UITouch *> *)touches withEvent:(UIEvent *)event {
    self.player.volume = self.player.volume + 0.1;
}

- (void)close {
    self.navigationController.navigationBar.hidden = NO;
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    [UIView animateWithDuration:0.25 animations: ^{
        self.player.transform = transform;
        self.player.frame = CGRectMake(0, 64, self.view.bounds.size.width, 250);
    }];
    
}

- (void)changValue {
    [self.player pause];
}

- (void)jumpValue {
    self.player.jumpToTime = self.slider.value;
}

- (void)dealloc {
    NSLog(@"======页面已经释放=======");
    [self.player removeObserver:self forKeyPath:@"totalTime"];
    [self.player removeObserver:self forKeyPath:@"bufferTime"];
    [self.player removeObserver:self forKeyPath:@"currentTime"];
    [self.player reset];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary <NSString *, id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"totalTime"]) {
        NSLog(@"总时间:%@", change[@"new"]);
        self.slider.minimumValue = 0;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0f);
        UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [self.slider setMinimumTrackImage:transparentImage forState:UIControlStateNormal];
        [self.slider setMaximumTrackImage:transparentImage forState:UIControlStateNormal];
        self.slider.maximumValue = [change[@"new"] doubleValue];
    }
    else if ([keyPath isEqualToString:@"bufferTime"]) {
        NSLog(@"缓存时间:%@", change[@"new"]);
        self.progress.progress = [change[@"new"] doubleValue] / self.player.totalTime;
    }
    else if ([keyPath isEqualToString:@"currentTime"]) {
        NSLog(@"当前时间:%@", change[@"new"]);
        [self.slider setValue: [change[@"new"] doubleValue] animated:YES];
    }
}

- (IBAction)pause:(id)sender {
    [self.player pause];
}
- (IBAction)play:(id)sender {
    [self.player play];
}
- (IBAction)reset:(id)sender {
    [self.player reset];
}

- (IBAction)jump:(id)sender {
    CGFloat time = [self.time.text doubleValue];
    self.player.jumpToTime = time;
}

- (IBAction)quanp:(id)sender {
    self.navigationController.navigationBar.hidden = YES;
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 2);
    [UIView animateWithDuration:0.25 animations: ^{
        self.player.transform = transform;
    }];
    [UIView animateWithDuration:0.25 animations:^{
        self.player.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    }];
}



@end
